package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TlCity;
import com.qf.entity.TlHotel;
import com.qf.mapper.AdminHotelMapper;
import com.qf.mapper.CityMapper;
import com.qf.service.AdminHotelService;
import com.qf.utils.Mycode;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminHotelServiceImpl extends ServiceImpl<AdminHotelMapper, TlHotel> implements AdminHotelService {
    @Autowired
    CityMapper cityMapper;
    /**
     * 查询所有酒店
     * @return
     */
    @Override
    public ResultMsg findAllHotel(int page, int limit) {
        Page<TlHotel> hotel = new Page<>(page, limit);
        Page<TlHotel> page1 = super.page(hotel);

        List<TlHotel> records = page1.getRecords();
        for (TlHotel record : records) {
            Integer cityId = record.getCityId();
            TlCity tlCity = cityMapper.selectById(cityId);
            record.setCityName(tlCity.getCityName());
        }

        return ResultMsg.SUCCESS(page1);

    }

    @Override
    public ResultMsg findone(int hid) {
        TlHotel byId = super.getById(hid);
        return ResultMsg.SUCCESS(byId);
    }

    @Override
    public ResultMsg update(TlHotel hotel) {
        boolean b = super.saveOrUpdate(hotel);
      if (b)
          return ResultMsg.SUCCESS();
      return ResultMsg.ERROR(Mycode.UPDATE_ERRO);

    }

    @Override
    public ResultMsg delet(int hid) {
        boolean b = super.removeById(hid);
        if (b)
            return ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.DELETEONE_ERRO);
    }
}
